<%--
  Created by IntelliJ IDEA.
  User: Jamin
  Date: 2019/1/23
  Time: 14:47
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>优惠券管理</title>
    <script src="../../layui-2.4/layui-v2.4.5/layui/layui.js" charset="utf-8"></script>
    <link rel="stylesheet" href="../../layui-2.4/layui-v2.4.5/layui/css/layui.css">
    <script src="../../js/jquery.min.3.1.0.js"></script>
    <!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
</head>
<body>

<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
        <button class="layui-btn layui-btn-sm" lay-event="insert">新增</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<script>
    layui.use('table', function () {
        var table = layui.table;

        table.render({
            elem: '#test'
            , url: '/CouponServlet'
            , toolbar: '#toolbarDemo'
            , title: '优惠券管理表'
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'cID', title: 'ID', width: 80, fixed: 'left', unresize: true, sort: true}
                , {field: 'cDescription', title: '描述', width: 160, fixed: 'left', unresize: true, sort: true}
                , {field: 'cTerm', title: '满额条件', width: 80, fixed: 'left', unresize: true, sort: true}
                , {field: 'cVipPrice', title: '满减', width: 80, fixed: 'left', unresize: true, sort: true}
                , {field: 'cDeadline', title: '截至时间', width: 160, fixed: 'left', unresize: true, sort: true}
                , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 150}

            ]]
            , page: true
            , limits: [2, 4, 8, 10, 15, 20]
        });

        //头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：' + data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选' : '未全选');
                    break;
                case  'insert':
                    parent.layer.open({   //打开一个弹出层
                        title: '新增信息',   //弹出层的标题
                        maxmin: true,
                        type: 2,
                        content: 'InsertCoupon.jsp',//要访问的地址
                        area: ['800px', '600px'],
                        end: function (data) { //表示弹出层关闭的时候的代码块
                            location.reload();
                        }
                    });
                    break;
            }
            ;
        });

        //监听行工具事件
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            //console.log(obj)
            if (obj.event === 'del') {
                var con = confirm("真的要删除吗");
                if (con) {
                    $.ajax({
                        url: "/DeleteCoupon?id=" + data.cID,   //请求路径
                        datatype: "json",//返回的格式
                        type: "POST", //什么请求
                        success: function (data) { //请求成功以后
                            window.location.href = "Coupon.jsp";
                        }
                    });
                }

            } else if (obj.event === 'edit') {
                parent.layer.open({   //打开一个弹出层
                    title: '修改信息',   //弹出层的标题
                    maxmin: true,
                    type: 2,
                    content: '/CouponUpload?id=' + data.cID,//要访问的地址
                    area: ['800px', '600px'],
                    end: function () { //表示弹出层关闭的时候的代码块
                        location.reload();
                    }
                });
            }


        });
    })
    ;
</script>

</body>
</html>
